<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>register</title>
    <link rel="stylesheet" href="../statics/css/bootstrap.min copy.css" media="all">
</head>
<style>
    .wrap {
        width: 100%
    }

    .container {
        width: 1070px;
        height: 100px;
        margin: 0 auto;
    }

    .col-2 {
        padding-top: 7px;
        font-family: fantasy;
        font-size: 20px;
        color: black;
        margin-left: 65px;
    }

    .form-check {
        padding-left: 7rem;
    }

    .form-group {
        margin-left: 5rem;
    }

    .align-middle {
        margin-left: 16rem;
        margin-bottom: 2rem;
    }

    .btn {
        margin-left: 9rem;
        color: black;
        background-color: #fff;
        font-family: fantasy;
        font-size: 30px;
    }

    .form-control {

        border-bottom: black solid 1px;
    }
</style>

<body background="../statics/Photo/City.jpg" style="background-repeat: no-repeat;
background-size: 100% 100%;
background-attachment: fixed;">
    <div class="wrap">
        <div class="container">
            <div class="row align-items-center">
                <div class="col">

                </div>
                <div class="col">
                    <div class="alert alert-danger alert-dismissible fade show invisible text-center"
                        style="width: 42rem;top: 100px;right: -10px;" role="alert">
                        <strong>Tips:</strong><label for="" id="msg">??</label>
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                </div>
                <div class="col">

                </div>
            </div>
        </div>
        <div class="row align-items-center" style="height: 50rem;">
            <div class="col">

            </div>
            <div class="col">
                <div style="width: 42rem;">
                    <div class="card-body align-items-center">
                        <h2 class="align-middle" style="font-family: fantasy;font-size: 40px;margin-left: 130px;">
                            Register
                            your Life📕</h2>
                        <br>
                        <br><br>
                        <form id="registerForm" action="/registerDone" method="POST">

                            <div class="form-group row">
                                <label class="col-2" for="exampleInputEmail1">User</label>
                                <input type="text" class="form-control col-6" name="username" id="username"
                                    aria-describedby="emailHelp">
                            </div>
                            <br>
                            <br>


                            <div class="form-group row">
                                <label class="col-2" for="exampleInputPassword1">PWD</label>
                                <input type="password" class="form-control col-6" name="password" id="password">
                            </div>
                            <br>
                            <br>
                            <div class="form-group row">
                                <label class="col-2" for="exampleInputPassword1">REPWD</label>
                                <input type="password" class="form-control col-6" name="surePasswords"
                                    id="surePasswords">
                            </div>
                            <br>
                            <br>

                            <button type="button" class="btn btn-primary" onclick="register()">Register</button>
                            <button type="button" class="btn btn-primary" onclick="Cancle()">Cancle</button>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col">

            </div>
           
        </div>
    </div>

    <script src="../statics/js/jquery-3.6.0.min.js"></script>
    <script src="../statics/js/bootstrap.min.js"></script>

    <script>
        function register() {
            let username = $('#username').val();
            let password = $('#password').val();
            let surePasswords = $('#surePasswords').val();
            if (username.length > 0 && password.length > 0 && password === surePasswords) {
                // ajax请求
                $.ajax({
                    url: "/registerDone",
                    type: "POST",
                    data: $('#registerForm').serialize(),
                    dataType: "json",
                    success: function (res) {
                        console.log(res);
                        if (res.code === 200) {
                            window.location.href = "/Login";
                        } else {
                            alert_fn(res.msg);
                        }

                    },
                    error: function (msg) {
                        alert_fn('Net Incorrect');
                    }
                })
            } else {
                alert_fn();
                return false;
            }
        }

        function alert_fn(msg) {
            msg = msg || '密码不能为空，两次密码应该一致';
            $('#msg').text(msg);
            $('.invisible').removeClass('invisible');
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 5000);
        }

        function Cancle() {
            window.location.href = '/Login'
        }

    </script>
</body>

</html>